<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" @filterChange="handleSearch" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="showtempRef"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onSyncTableData="handleSyncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <div class="btnWrap">
      <el-button type="primary" @click="closeDrawer">取消</el-button>
      <!-- <el-button type="primary" @click="closeDrawer">打印</el-button> -->
      <!-- <el-button type="primary" @click="account">出库</el-button> -->
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile, arrayObjDeWeight } from '@/utils/index';
import { getAllUser } from '@/api/customerRepair/usedCarVisit/resultEntry';
import { queryUnitCode } from '@/api/repair/batchEventManagement/activityDistribution';
import { confirmAction, notifyAction } from '@/utils';
import { accountLossOrder, getLossOrderItems } from '@/api/part/tools';
import { getStroageList } from '@/api/part/partOutbound/partBreak';

export default {
  mixins: [dictionary],
  mounted() {
    this.getAllUser();
    this.getUnit();
    this.setForm();
    this.searchstore();
  },
  props: ['rowData'],
  data() {
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      fetchapi: getLossOrderItems,
      fetchParams: { xhrAbort: true },
      loading: false,
      showPanel: false,
      syncTableData: [] // 同步的table数据
    };
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '报损单号',
          fieldName: 'lossNo',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '报损类型',
          fieldName: 'orderType',
          disabled: true,
          itemList: [
            { text: '借用报损', value: 82491030 },
            { text: '库存报损', value: 82491031 }
          ]
        },
        {
          type: 'INPUT',
          label: '借用单号',
          fieldName: 'borrowNo',
          readonly: true,
          disabled: true,
          unitRender: () => {
            return (
              <el-button
                icon="el-icon-search"
                onClick={() => {
                  this.isShowSearchBorrowOrder = true;
                }}
              ></el-button>
            );
          }
        },
        {
          type: 'SELECT',
          label: '经手人',
          fieldName: 'lossUser',
          itemList: [],
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remarks',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '工具代码',
          dataIndex: 'toolCode',
          sorter: true
        },
        {
          title: '工具名称',
          dataIndex: 'toolName',
          sorter: true
        },
        {
          title: '仓库名称',
          dictItems: [],
          dataIndex: 'storageCode',
          sorter: true
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          sorter: true
        },
        {
          title: '单位',
          dataIndex: 'unit',
          filterType: 'checkbox',
          dictItems: [],
          sorter: true
        },
        {
          title: '报损数量',
          dataIndex: 'counts',
          filterType: 'number',
          min: 0,
          precision: 2,
          editType: 'number',
          sorter: true,
          filter: true
        }
      ];
    },
    closeDrawer() {
      this.$emit('close');
      this.deleteList = [];
    },
    async getAllUser() {
      const res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.topFilterList.find(item => item.fieldName === 'lossUser').itemList = data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    async account() {
      console.log('account::');
      this.loading = true;
      let param = { orderNo: this.topFilterList.find(x => x.fieldName == 'lossNo').initialValue };
      const res = await accountLossOrder(param);
      if (res.resultCode == 200) {
        this.$notify({
          title: '成功',
          message: '出库成功',
          type: 'success'
        });
      }
      this.loading = false;
      this.closeDrawer();
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    async searchstore() {
      const res = await getStroageList();
      this.codeName = this.columns.find(x => x.dataIndex == 'storageCode').dictItems = this.columns.find(x => x.dataIndex == 'storageCode').filterItems = this.columns.find(
        x => x.dataIndex == 'storageCode'
      ).editItems = res.data.map(x => ({
        value: x.STORAGE_CODE,
        text: x.STORAGE_NAME
      }));
    },
    // 点击搜索
    handleSearch() {},
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    async setForm() {
      if (this.rowData) {
        this.topFilterList.find(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        let lossNo = this.rowData.lossNo;
        let type = this.rowData.orderType;
        this.fetchParams = Object.assign({ xhrAbort: false }, { orderNo: lossNo, type: type });
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async getUnit() {
      const res = await queryUnitCode();
      console.log('getUnit res::', res);
      let data = arrayObjDeWeight(res.data, 'UNIT_CODE');
      this.columns.find(x => x.dataIndex == 'unit').dictItems = data.map(x => ({ value: x.UNIT_CODE + '', text: x.UNIT_NAME }));
    }
  }
};
</script>

<style lang="less" scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
